<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<title>Simulating Transducer Field Patterns Example (k-Wave)</title>
	<link rel="stylesheet" href="kwavehelpstyle.css" type="text/css">
	<meta name="description" content="Simulating Transducer Field Patterns Example.">
</head>

<body><div class="content">

<h1>Simulating Transducer Field Patterns Example</h1>

<p>This example demonstrates the use of k-Wave to compute the field pattern generated by a curved single element transducer in two dimensions. It builds on the <a href="example_tvsp_homogeneous_medium_monopole.html">Monopole Point Source In A Homogeneous Propagation Medium</a>, <a href="example_ivp_recording_particle_velocity.html">Recording The Particle Velocity</a>, and <a href="example_ivp_opposing_corners_sensor_mask.html">Defining A Sensor Mask By Opposing Corners</a> Examples.</p>

<p>
    <ul>
        <li><a href="matlab:edit([getkWavePath('examples') 'example_tvsp_transducer_field_patterns.m']);" target="_top">Open the file in the MATLAB Editor</a></li>
        <li><a href="matlab:run([getkWavePath('examples') 'example_tvsp_transducer_field_patterns']);" target="_top">Run the file in MATLAB</a></li>
    </ul>
</p>

<h2>Contents</h2>
<div>
	<ul>
        <li><a href="#heading2">Defining the time varying pressure source</a></li>
        <li><a href="#heading3">Recording the statistics of the pressure field</a></li>
        <li><a href="#heading4">Running the simulation</a></li>
	</ul>
</div>

<a name="heading2"></a>
<h2>Defining the time varying pressure source</h2>

<p>As in the previous examples, a time varying pressure source is defined by assigning a binary source mask to <code>source.p_mask</code> (which defines the position of the source points) along with a time varying source input to <code>source.p</code>. Here a single sinusoidal time series is used to drive a curved transducer element.</p>

<pre class="codeinput">
<span class="comment">% define a curved transducer element</span>
arc_pos = [20, 20];         <span class="comment">% [grid points]</span>  
radius = 60;                <span class="comment">% [grid points]</span>
diameter = 81;              <span class="comment">% [grid points]</span>
focus_pos = [Nx/2, Nx/2];   <span class="comment">% [grid points]</span>
source.p_mask = makeArc([Nx, Ny], arc_pos, radius, diameter, focus_pos);

<span class="comment">% define a time varying sinusoidal source</span>
source_freq = 0.25e6;       <span class="comment">% [Hz]</span>
source_mag = 0.5;           <span class="comment">% [Pa]</span>
source.p = source_mag * sin(2 * pi * source_freq * kgrid.t_array);

<span class="comment">% filter the source to remove any high frequencies not supported by the grid</span>
source.p = filterTimeSeries(kgrid, medium, source.p);
</pre>

<a name="heading3"></a>
<h2>Recording the statistics of the pressure field</h2>

<p>To visualise the acoustic beam produced by the curved transducer, a sensor mask covering the entire computational domain is defined using the grid coordinates of two opposing corners of a rectangle (a list of the different sensor mask types is given in the <a href="example_ivp_homogeneous_medium.html">Homogeneous Propagation Medium Example</a>). If only the total beam pattern is required (rather than the beam pattern at particular frequencies or times), this can be produced without having to store the complete time series at each sensor point by setting <code>sensor.record</code> to <code> {'p_final', 'p_max', 'p_rms'}</code>. With this option, at each time step k-Wave only updates the maximum and rms values of the pressure at each sensor point. This can significantly reduce the memory requirements for storing the sensor data, particularly if sensor masks with large numbers of sensor points are used. </p>

<pre class="codeinput">
<span class="comment">% create a sensor mask covering the entire computational domain using the
% opposing corners of a rectangle</span>
sensor.mask = [1, 1, Nx, Ny].';

<span class="comment">% set the record mode to capture the final wave-field and the statistics at
% each sensor point</span>
sensor.record = {'p_final', 'p_max', 'p_rms'};
</pre>

<p>Note, if recording the maximum or minimum pressure everywhere, it is also possible to set <code>sensor.record</code> to <code> {'p_max_all', 'p_min_all'}</code>. This returns the maximum and minimum pressure everywhere, regardless of the shape or type of sensor mask. Consequently, provided no other outputs are required, in this case it is possible to leave the sensor mask blank by setting <code>sensor.mask = []</code>.</p>

<a name="heading4"></a>
<h2>Running the simulation</h2>

<p>The simulation is again invoked by calling <code><a href="kspaceFirstOrder2D.html">kspaceFirstOrder2D</a></code>. To allow visualisation of the source elements within the grid, the source mask is assigned to the optional input <code>'DisplayMask'</code>. This mask is overlaid onto the plot during the simulation. The PML is also set to be outside the computational grid defined by the user and then hidden from display by setting the optional inputs <code>'PlotPML'</code> and <code>'PMLInside'</code> to <code>false</code>.</p>

<pre class="codeinput">
<span class="comment">% create a display mask to display the transducer</span>
display_mask = source.p_mask;

<span class="comment">% assign the input options</span>
input_args = {'DisplayMask', display_mask, 'PMLInside', false, 'PlotPML', false};

<span class="comment">% run the simulation</span>
sensor_data = kspaceFirstOrder2D(kgrid, medium, source, sensor, input_args{:});
</pre>

<p>As described in the <a href="example_ivp_recording_particle_velocity.html">Recording The Particle Velocity Example</a>, if an input for <code>sensor.record</code> is defined, the output <code>sensor_data</code> returned from the simulation is defined as a structure, with the recorded acoustic variables appended as structure fields. In this case the output <code>sensor_data</code> is given as a structure with the fields <code>sensor_data.p_final</code>, <code>sensor_data.p_max</code>, and <code>sensor_data.p_rms</code>. For a sensor mask defined using the grid coordinates of opposing corners of a rectangle, <code>p_rms</code> and <code>p_max</code> are indexed as <code>(x_index, y_index)</code>, where <code>x_index</code> and <code>y_index</code> correspond to the grid index within the rectangle. The final pressure field along with the maximum and rms beam patterns are plotted below. Both the transducer focus and the side lobes are clearly visible.</p>

<img vspace="5" hspace="5" src="images/example_tvsp_transducer_field_patterns_01.png" style="width:1120px;height:420px;" alt="">

</div></body></html>